#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/bst.h"
#include "../include/test.h"
#include "../include/vfs_errorcodes.h"


node insert_bst(char *x, node t, int desc){
    //printf("m here in function");
    node temp; // bst node
    filenode templink, templink1; // link list node
    if(t==NULL){
	//printf("m here in if of function");
        t=(node) malloc(sizeof(struct BinarySearchTree));
	templink = (filenode) malloc(sizeof(struct FileLink));
        strcpy(t->str, x);
        t->left=NULL;
        t->right=NULL;
	t->FileDescNo=0;
	//printf("m here");
	t->center = templink;
	templink->next = NULL;
	templink->FileDescNo = desc;
	//printf("m here after");
    }
    else{
        if(strcmp(x, t->str)<0){
	    if(t->left==NULL){
		temp=(node) malloc(sizeof(struct BinarySearchTree));
		templink = (filenode) malloc(sizeof(struct FileLink));
	        strcpy(temp->str, x);
        	temp->left=NULL;
       		temp->right=NULL;
		temp->FileDescNo=0;
		temp->center = templink;
		templink->next = NULL;
		templink->FileDescNo = desc;
	    	t->left = temp;
	    }
	    else
            	insert_bst(x, t->left, desc);
	    }
        else if(strcmp(x, t->str)>0){
	    if(t->right==NULL){
		temp=(node) malloc(sizeof(struct BinarySearchTree));
		templink = (filenode) malloc(sizeof(struct FileLink));
	        strcpy(temp->str, x);
        	temp->left=NULL;
       		temp->right=NULL;
		temp->FileDescNo=0;
		temp->center = templink;
		templink->next = NULL;
		templink->FileDescNo = desc;
	    	t->right = temp;
	    }
	    else
            	insert_bst(x, t->right, desc);
	    }
	else{
		templink = (filenode) malloc(sizeof(struct FileLink));
		templink->next = t->center;
		templink->FileDescNo = desc;
		t->center = templink;  				
	}
    }
    return t;
}

node delete_bst(char *x, node t, int desc){
	node temp;
	filenode templink1, templink2;
	if(t == NULL) ;
		//printf("\nElement not found");
	else{
		if(strcmp(x,t->str)<0)
			t->left = delete_bst(x, t->left, desc);
		else if(strcmp(x,t->str)>0)
			t->right = delete_bst(x, t->right, desc);
		else{
			if(t->center->next == NULL){
				if(t->center->FileDescNo == desc){
					if(t->left && t->right){
						temp = findmin_bst(t->right);
						strcpy(t->str,temp->str);
						t->right = delete_bst(t->str,t->right,desc);
					}
					else if(t->left == NULL)
						t=t->right;
					else
						t=t->left;
				}
				
			}
			else if(t->center->FileDescNo == desc){
				t->center = t->center->next;
			}
			else{
				templink1 = t->center;
				templink2 = templink1;
				while(templink2 != NULL){
					templink1 = templink2;
					templink2 = templink2->next;
					if(templink2->FileDescNo == desc)
						break;
				}
				if(templink2 = NULL)
					
					templink1->next = templink2->next;
			}
		}
	}
	return t;
}


node findmin_bst(node temp){
	if(temp == NULL || temp->left == NULL)
		return temp;
	return findmin_bst(temp->left);
}

/*
node find_bst(char *x, node t){
	if(t==NULL) 
		return NULL;
	if(strcmp(x,t->str)<0) 
		return find_bst(x,t->left);
	if(strcmp(x,t->str)>0) 
		return find_bst(x,t->right);
	return t;
}
*/

char * search_with_filename(char * file_name,char * dest_path)
{
  node temp;
  FILE * fp;
  searchnode templink;
  searchroot = NULL;
  inorder(bst, file_name);
  //temp = find_bst(file_name, bst);
  int no_of_occ=0;
  if((fp=fopen(dest_path,"w"))==NULL)
  {
                  strcpy(error,"searchfile_FAILURE ");
		  strcat(error,ERR_VFS_SEARCHFILE_01);
                  return error;

  }
				
				if(searchroot == NULL){
					//printf("\nInside here");
					fprintf(fp,"File does not exists");
				}
				else{
					templink = searchroot;
					while(templink != NULL){
						//printf("%d\n ",templink->search_FileDescNo);
						//print the path of parent to file
				                fprintf(fp,"%s\n",header.fd[header.fd[templink->search_FileDescNo].parent_file_descriptor_no].path);		
						templink = templink->searchnext;
						no_of_occ++;
						}
					}

   sprintf(error,"searchfile_SUCCESS %d",no_of_occ);
   return error;
}

// searchroot is the root of the link list that contains the search result make it global in the bst.h file...
void inorder(node t, char *strtemp){  // function to search for string begining with some characters....
	filenode templink; // link list node
	searchnode newnode; // search link list node
	int length;
	if(t==NULL);
	else{
		length = strlen(strtemp);
		inorder(t->left, strtemp);
		if(strncmp(strtemp,t->str,length) == 0){
			if(t->center != NULL){
				templink = t->center;
				while(templink != NULL){
					newnode = (searchnode) malloc(sizeof(struct searchlist));
					newnode->search_FileDescNo = templink->FileDescNo;
					strcpy(newnode->searchstr,t->str);
					newnode->searchnext = NULL;
					if(searchroot == NULL){
						searchroot = newnode;
					}
					else{
						newnode->searchnext = searchroot;
						searchroot = newnode;
					}
					templink = templink->next;
				}
			}

		}
		inorder(t->right, strtemp);
	}		
}
